﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Clipper.Model.DataBaseModel;

namespace Clipper.DAL
{
    public class LocaltionPosition
    {

        /// <summary>
        /// 查询一个对象
        /// </summary>
        /// <param name="placeName"></param>
        /// <returns></returns>
        public Clipper.Model.DataBaseModel.LocationPosition GetModel(string placeName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select place,Y_axis,X_axis,Z_axis,Z1_axis,Z2_axis,X_offset,Y_offset,cleanTime,Z1_Z2Offset from LocationPosition");
            strSql.Append(" where place=@placeName");

            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@placeName", DbType.String)
            };
            parameters[0].Value = placeName;

            Clipper.Model.DataBaseModel.LocationPosition model = new Clipper.Model.DataBaseModel.LocationPosition();
            DataSet ds = SQLiteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }

        }

        public LocationPosition DataRowToModel(DataRow dataRow)
        {
            LocationPosition lacaltionModel = new LocationPosition();
            if (dataRow != null)
            {
                if (dataRow["place"] != null)
                {
                    lacaltionModel.Place = dataRow["place"].ToString();
                }
                if (dataRow["X_axis"] != null && dataRow["X_axis"].ToString() != "")
                {
                    lacaltionModel.XAxis = int.Parse(dataRow["X_axis"].ToString());
                }
                if (dataRow["Y_axis"] != null && dataRow["Y_axis"].ToString() != "")
                {
                    lacaltionModel.YAxis = int.Parse(dataRow["Y_axis"].ToString());
                }
                if (dataRow["Z_axis"] != null && dataRow["Z_axis"].ToString() != "")
                {
                    lacaltionModel.ZAxis = int.Parse(dataRow["Z_axis"].ToString());
                }
                if (dataRow["Z1_axis"] != null && dataRow["Z1_axis"].ToString() != "")
                {
                    lacaltionModel.Z1Axis = int.Parse(dataRow["Z1_axis"].ToString());
                }
                if (dataRow["Z2_axis"] != null && dataRow["Z2_axis"].ToString() != "")
                {
                    lacaltionModel.Z2Axis = int.Parse(dataRow["Z2_axis"].ToString());
                }
                if (dataRow["X_offset"] != null && dataRow["X_offset"].ToString() != "")
                {
                    lacaltionModel.XOffset = int.Parse(dataRow["X_offset"].ToString());
                }
                if (dataRow["Y_offset"] != null && dataRow["Y_offset"].ToString() != "")
                {
                    lacaltionModel.YOffset = int.Parse(dataRow["Y_offset"].ToString());
                }
                if (dataRow["cleanTime"] != null && dataRow["cleanTime"].ToString() != "")
                {
                    lacaltionModel.CleanTime = int.Parse(dataRow["cleanTime"].ToString());
                }
                if (dataRow["Z1_Z2Offset"] != null && dataRow["Z1_Z2Offset"].ToString() != "")
                {
                    lacaltionModel.Z1_z2Offset = int.Parse(dataRow["Z1_Z2Offset"].ToString());
                }

            }
            return lacaltionModel;
        }
        /// <summary>
        /// 更新表中的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(LocationPosition model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LocationPosition set ");
            strSql.Append("Y_axis=@Y_axis,");
            strSql.Append("X_axis=@X_axis,");
            strSql.Append("Z_axis=@Z_axis,");
            strSql.Append("Z1_axis=@Z1_axis,");
            strSql.Append("Z2_axis=@Z2_axis,");
            strSql.Append("Y_offset=@Y_offset,");
            strSql.Append("X_offset=@X_offset,");
            strSql.Append("cleanTime=@cleanTime,");
            strSql.Append("Z1_Z2Offset=@Z1_Z2Offset ");
            strSql.Append("where place=@place ");

            SQLiteParameter[] parameters = {
                new SQLiteParameter("@Y_axis",DbType.Int32),
                new SQLiteParameter("@X_axis",DbType.Int32),
                new SQLiteParameter("@Z_axis",DbType.Int32),
                new SQLiteParameter("@Z1_axis",DbType.Int32),
                new SQLiteParameter("@Z2_axis",DbType.Int32),
                new SQLiteParameter("@X_offset",DbType.Int32),
                new SQLiteParameter("@Y_offset",DbType.Int32),
                new SQLiteParameter("@cleanTime",DbType.Int32),
                new SQLiteParameter("@Z1_Z2Offset",DbType.Int32),
                new SQLiteParameter("@place",DbType.String)
            };
            parameters[0].Value = model.YAxis;
            parameters[1].Value = model.XAxis;
            parameters[2].Value = model.ZAxis;
            parameters[3].Value = model.Z1Axis;
            parameters[4].Value = model.Z2Axis;
            parameters[5].Value = model.XOffset;
            parameters[6].Value = model.YOffset;
            parameters[7].Value = model.CleanTime;
            parameters[8].Value = model.Z1_z2Offset;
            parameters[9].Value = model.Place;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select place,Y_axis,X_axis,Z_axis,Z1_axis,Z2_axis,Y_offset,X_offset,cleanTime,Z1_Z2Offset ");
            strSql.Append(" FROM LocationPosition ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteHelper.Query(strSql.ToString());
        }
    }
}
